﻿using System;



namespace EmployeeManagementSystem

{

    // 定义员工类

    class Employee

    {

        // 成员变量

        private int id;

        private string name;

        private int age;

        private string department;

        private string position;

        private double salary;

        private DateTime hireDate;



        // 无参构造方法

        public Employee()

        {

            this.id = 0;

            this.name = "未知";

            this.age = 0;

            this.department = "未分配";

            this.position = "未指定";

            this.salary = 0.0;

            this.hireDate = DateTime.Now;

        }



        // 带参构造方法

        public Employee(int id, string name, int age, string department, string position, double salary, DateTime hireDate)

        {

            this.id = id;

            this.name = name;

            this.age = age;

            this.department = department;

            this.position = position;

            this.salary = salary;

            this.hireDate = hireDate;

        }



        // 属性访问器 (getXxx()/setXxx() 方法)

        public int Id

        {

            get { return id; }

            set { id = value; }

        }



        public string Name

        {

            get { return name; }

            set { name = value; }

        }



        public int Age

        {

            get { return age; }

            set { age = value > 0 ? value : 0; } // 年龄必须大于0

        }



        public string Department

        {

            get { return department; }

            set { department = value; }

        }



        public string Position

        {

            get { return position; }

            set { position = value; }

        }



        public double Salary

        {

            get { return salary; }

            set { salary = value >= 0 ? value : 0; } // 工资不能为负数

        }



        public DateTime HireDate

        {

            get { return hireDate; }

            set { hireDate = value; }

        }



        // 显示所有成员信息的方法

        public void DisplayInfo()

        {

            Console.WriteLine("===== 员工信息 =====");

            Console.WriteLine($"员工编号: {Id}");

            Console.WriteLine($"姓名: {Name}");

            Console.WriteLine($"年龄: {Age}");

            Console.WriteLine($"部门: {Department}");

            Console.WriteLine($"职位: {Position}");

            Console.WriteLine($"工资: {Salary:C}");

            Console.WriteLine($"入职日期: {HireDate:yyyy-MM-dd}");

            Console.WriteLine("====================");

            Console.WriteLine();

        }



        // 计算工龄的方法（额外功能）

        public int CalculateYearsOfService()

        {

            DateTime today = DateTime.Today;

            int years = today.Year - HireDate.Year;



            // 如果今年还没到入职日期，则减一年

            if (HireDate.Date > today.AddYears(-years))

            {

                years--;

            }



            return years;

        }

    }



    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("员工管理系统测试");

            Console.WriteLine("=================");

            Console.WriteLine();



            // 测试无参构造方法

            Employee emp1 = new Employee();

            Console.WriteLine("使用无参构造方法创建的员工:");

            emp1.DisplayInfo();



            // 测试带参构造方法

            Employee emp2 = new Employee(1001, "张三", 28, "技术部", "软件工程师", 15000.0, new DateTime(2020, 5, 15));

            Console.WriteLine("使用带参构造方法创建的员工:");

            emp2.DisplayInfo();

            Console.WriteLine($"工龄: {emp2.CalculateYearsOfService()} 年");

            Console.WriteLine();



            // 测试属性访问器

            Employee emp3 = new Employee();

            emp3.Id = 1002;

            emp3.Name = "李四";

            emp3.Age = 32;

            emp3.Department = "市场部";

            emp3.Position = "市场经理";

            emp3.Salary = 20000.0;

            emp3.HireDate = new DateTime(2018, 8, 20);



            Console.WriteLine("使用属性访问器设置的员工:");

            emp3.DisplayInfo();

            Console.WriteLine($"工龄: {emp3.CalculateYearsOfService()} 年");

            Console.WriteLine();



            // 测试边界情况

            Employee emp4 = new Employee(1003, "王五", -5, "财务部", "会计", -5000.0, DateTime.Now);

            Console.WriteLine("测试边界情况（年龄和工资为负）:");

            emp4.DisplayInfo(); // 年龄和工资会被自动校正为0

            Console.WriteLine();



            // 测试员工列表

            Console.WriteLine("员工列表:");

            Employee[] employees = { emp1, emp2, emp3, emp4 };



            foreach (Employee emp in employees)

            {

                Console.WriteLine($"{emp.Id}\t{emp.Name}\t{emp.Department}\t{emp.Position}\t{emp.Salary:C}");

            }



            Console.ReadLine();

        }

    }

}